﻿using System.Data.EntityClient;
using System.Data.SqlClient;
using ReagentStore.ERM.Properties;

namespace ReagentStore.ERM
{
	public class ConnectionStringBuilder
	{
		private const string _metadata =
			@"res://*/ReagentStoreContainer.csdl|res://*/ReagentStoreContainer.ssdl|res://*/ReagentStoreContainer.msl";

		private const string _provider = @"System.Data.SqlClient";
		private const string _database = "ReagentStore";
		private const string _master = "Master";
		private readonly string _server = Settings.Default.ServerName;

		public string EntityConnectionString()
		{
			var entityconnection = new EntityConnectionStringBuilder
			                       	{
			                       		Provider = _provider,
			                       		ProviderConnectionString = SqlConnectionString(_database),
			                       		Metadata = _metadata
			                       	};

			return entityconnection.ToString();
		}

		private string SqlConnectionString(string database)
		{
			var sqlconnection = new SqlConnectionStringBuilder
			                    	{
			                    		DataSource = _server,
			                    		InitialCatalog = database,
			                    		IntegratedSecurity = true,
			                    		MultipleActiveResultSets = true,
			                    		ApplicationName = "EntityFramework"
			                    	};

			return sqlconnection.ToString();
		}

		public string MaintainConnectionString()
		{
			return SqlConnectionString(_master);
		}
	}
}